package com.simple.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordRegionList {
	
	private List<WordRegion> wordRegions = new ArrayList<>();
	
	private Map<String, WordRegion> regionMap = new HashMap<>();
	
	public WordRegionList(WordRegion wordRegion) {
		addWordRegionLast(wordRegion);
	}
	
	public void addWordRegionFirst(WordRegion wordRegion) {
		wordRegions.add(0, wordRegion);
		regionMap.put(wordRegion.getBoundingBox(), wordRegion);
	}
	
	public void addWordRegionLast(WordRegion wordRegion) {
		wordRegions.add(wordRegion);
		regionMap.put(wordRegion.getBoundingBox(), wordRegion);
	}
	
	public WordRegion getFirst() {
		return wordRegions.get(0);
	}
	
	public WordRegion getLast() {
		return wordRegions.get(wordRegions.size() - 1);
	}

	public void addAllLast(WordRegionList wordRegionList2) {
		List<WordRegion> list = new ArrayList<>(this.wordRegions.size() + wordRegionList2.wordRegions.size());
		list.addAll(wordRegions);
		list.addAll(wordRegionList2.wordRegions);
		wordRegions = list;
		for(String key: wordRegionList2.regionMap.keySet()) {
			regionMap.put(key, wordRegionList2.regionMap.get(key));
		}
	}
	
	public void addAllFirst(WordRegionList wordRegionList2) {
		List<WordRegion> list = new ArrayList<>(this.wordRegions.size() + wordRegionList2.wordRegions.size());
		list.addAll(wordRegionList2.wordRegions);
		list.addAll(wordRegions);
		wordRegions = list;
		for(String key: wordRegionList2.regionMap.keySet()) {
			regionMap.put(key, wordRegionList2.regionMap.get(key));
		}
	}

	public Map<String, WordRegion> getRegionMap() {
		return regionMap;
	}

	public int getMinY() {
		int minY = Integer.MAX_VALUE;
		for(WordRegion wordRegion: wordRegions) {
			if(wordRegion.getTopY() < minY) {
				minY = wordRegion.getTopY();
			}
		}
		return minY;
	}
	
	public int getMaxY() {
		int maxY = 0;
		for(WordRegion wordRegion: wordRegions) {
			if(wordRegion.getBottomY() > maxY) {
				maxY = wordRegion.getBottomY();
			}
		}
		return maxY;
	}
}
